package com.pdsu.graduation_design.views;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.MyBuysGridViewAdapter;
import com.pdsu.graduation_design.adapter.MyToolsGridViewAdapter;

public class My_Fragment extends Fragment {

    private ImageView logout_imv;
    /**
     * 个人信息页
     */

    private TextView my_username;
    /**
     * 我的订单
     */

    private Context context;
    private GridView my_buys_gv;

    /**
     * 必备工具
     */
    private GridView my_tools_gv;

    private View currentView;

    private TextView bar_top_textview;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        currentView=inflater.inflate(R.layout.fragment_my,null);
        init();
        operation();
        bind();
        return currentView;
    }
    private void operation() {

//        bar_top_textview.setText("我");

        //设置监事件
        my_buys_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                System.out.println(position);
                switch (position){
                    case 0:
                        intent=new Intent(context,OrderListActivity.class);
                        break;
                    case 1:
                        intent=new Intent(context,LoadingOrderListActivity.class);
                        break;
                    case 2:
                        intent=new Intent(context,TransOrderListActivity.class);
                        break;
                    case 3:
                    case 4:
                        intent=new Intent(context,LoadingActivity.class);
                        break;

                }
                startActivity(intent);


            }
        });

        my_tools_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                System.out.println(position);
                switch (position){
                    case 0:
                        intent=new Intent(context,My_Like_Activity.class);
                        break;
                    case 1:
                        intent=new Intent(context,My_Address_Activity.class);
                        break;
                    case 2:
                        intent=new Intent(context,My_Coupons_Activity.class);
                        break;
                    case 3:
                        intent=new Intent(context,My_FeadBack_Activity.class);
                        break;
                    case  4:
                        intent = new Intent(context,My_Up_Commodity_Activity.class);
                        break;
                }
                startActivity(intent);
            }
        });

    }

    private void init() {
        logout_imv = currentView.findViewById(R.id.logout_imv);
        my_username=currentView.findViewById(R.id.my_username_tv);

        context=getContext();
        bar_top_textview = currentView.findViewById(R.id.bar_top_common_textview);

        //订单gv
        my_buys_gv=currentView.findViewById(R.id.my_buys_gv);

        //工具gv
        my_tools_gv=currentView.findViewById(R.id.my_tools_gv);

    }

    private void bind(){
        my_username.setText(MainActivity.user_admin.getUsername());

        //订单gv绑定适配器

        my_buys_gv.setAdapter(new MyBuysGridViewAdapter(currentView.getContext()));

        //工具gv绑定适配器
        my_tools_gv.setAdapter(new MyToolsGridViewAdapter(currentView.getContext()));

        logout_imv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /**
                 * 退出登录
                 */

//                context.startActivity();
            }
        });

    }

}
